// app/model/role_model.js
'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE, TEXT } = app.Sequelize;

  const Role = app.model.define('role', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: STRING(50),
      allowNull: false,
      unique: true,
      comment: '角色名称'
    },
    description: {
      type: STRING(200),
      allowNull: true,
      comment: '角色描述'
    },
    permissions: {
      type: TEXT,
      allowNull: true,
      comment: '权限列表',
      get() {
        const rawValue = this.getDataValue('permissions');
        return rawValue ? JSON.parse(rawValue) : [];
      },
      set(value) {
        this.setDataValue('permissions', value ? JSON.stringify(value) : '[]');
      }
    }
  }, {
    tableName: 'roles',
    underscored: true,
    comment: '角色表'
  });

  return Role;
};